package cn.ecnu.controller.recommend;

import cn.ecnu.common.utils.R;
import cn.ecnu.common.utils.TencentCloudUtil;
import cn.ecnu.common.utils.ThreadCandidate;
import cn.ecnu.common.utils.ThreadRecommender;
import cn.ecnu.pojo.personal.Title;
import cn.ecnu.pojo.recommend.RecommendCompany;
import cn.ecnu.pojo.recommend.RecommendJob;
import cn.ecnu.pojo.recommend.RecommendRequire;
import cn.ecnu.service.personal.TitleService;
import cn.ecnu.service.recommend.RecommendCompanyService;
import cn.ecnu.service.recommend.RecommendJobService;
import cn.ecnu.service.recommend.RecommendRequireService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Validated
@RestController
@RequestMapping("/recommend")
public class RecommendController {

    @Resource
    RecommendCompanyService recommendCompanyService;
    @Resource
    RecommendJobService recommendJobService;
    @Resource
    RecommendRequireService recommendRequireService;

    @PostMapping("/company")
    public R insertRecommendCompany(@Validated @RequestBody RecommendCompany recommendCompany){
        if (recommendCompanyService.saveOrUpdate(recommendCompany)){
            return R.ok();
        }
        return R.error("内推公司更新错误");
    }

    @GetMapping("/company")
    public R getRecommendCompany(@RequestParam("userId") Integer userId){
        RecommendCompany result = recommendCompanyService.getById(userId);
        if (result == null){
            return R.error("内推公司信息获取失败");
        }
        return R.data(result);
    }

    @DeleteMapping("/company")
    public R deleteRecommendCompany(@RequestParam("userId")Integer userId){
        if (recommendCompanyService.removeById(userId)) {
            return R.ok();
        }
        return R.error("内推公司信息删除失败");
    }



    @Resource
    TitleService titleService;
    @PostMapping("/job")
    public R insertRecommendJob(@Validated @RequestBody RecommendJob recommendJob){
        if (recommendJobService.saveOrUpdate(recommendJob)){
            titleService.update(Wrappers.<Title>lambdaUpdate()
                .eq(Title::getUserId, recommendJob.getUserId())
                .set(Title::getBulletinRecommender, recommendJob.getRecPosition()));
            return R.ok();
        }
        return R.error("内推岗位详情更新错误");
    }

    @GetMapping("/job")
    public R getRecommendJob(@RequestParam("userId") Integer userId){
        RecommendJob result = recommendJobService.getById(userId);
        if (result == null){
            return R.error("内推岗位详情获取失败");
        }
        return R.data(result);
    }



    @Resource
    ThreadRecommender threadRecommender;
    @PostMapping("/require")
    public R insertRecommendRequire(@Validated @RequestBody RecommendRequire recommendRequire){

        if (recommendRequireService.saveOrUpdate(recommendRequire)){
            threadRecommender.setRecommendRequire(recommendRequire);
            new Thread(threadRecommender).start();
            return R.ok();
        }
        return R.error("内推岗位详情更新错误");
    }

    @GetMapping("/require")
    public R getRecommendRequire(@RequestParam("userId") Integer userId){
        RecommendRequire result = recommendRequireService.getById(userId);
        if (result == null){
            return R.error("内推岗位详情获取失败");
        }
        return R.data(result);
    }

}
